function y_filt = filtsymm(filt_coeff, y_data);

% FILTSYMM - symmetrically filter data while minimizing edge effects
%
%    YF = FILTSYMM(A, YD) filters YD with a filter specified by
%    the coefficients in A.  Filtering is symmetrical with respect
%    to time, so that the filtered data has no phase shift.
%    This occurs by processing the input data in both the forward
%    and reverse directions.  After filtering in the forward
%    direction, the filtered data is reversed and filtered again.
%    Edge effects are minimized by padding the data with time-
%    reversed data from the beginning and end of the data.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: September 29, 2000

% check input arguments

if (nargin ~= 2)
   return
elseif (~isnumeric(filt_coeff))
   return
elseif (~isnumeric(y_data))
   return
end

% determine scaling for filtered data

if (sum(filt_coeff) > 0)
   filt_scale = sum(filt_coeff);
else
   filt_scale = 1;
end

% append time-reversed data

m = length(filt_coeff);
y_long = [fliplr(y_data(:, 1 : m)) y_data fliplr(y_data(:, end - m + 1 : end))];

% symmetrically filter data

y_long = filter(filt_coeff, 1, y_long, [], 2)/filt_scale;
y_long = fliplr(filter(filt_coeff, 1, fliplr(y_long), [], 2))/filt_scale;

% extract appropriate data points

y_filt = y_long(:, 1 + m : end - m);
return
